﻿using ChurchDiary.Common;
using ChurchDiary.DAL.Contract;
using ChurchDiary.DAL.DAL;
using ChurchDiary.TableEntity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChurchDiary.DAL.Methods
{
    public class DAL_OrganizationSettings : DataAccess, IOrganizationSettings
    {
        public string GetOrganizationStruture()
        {
            string OutPutJson = "";
            using (DbCommand dbcommand = SQLServer.GetStoredProcCommand("[dbo].[GET_OrganizationStructure]"))
            {
                using (DbConnection con = SQLServer.CreateConnection())
                {
                    using (IDataReader MReader = SQLServer.ExecuteReader(dbcommand))
                    {
                        if (null == MReader)
                        {
                            return OutPutJson;
                        }
                        else
                        {
                            while (MReader.Read())
                            {
                                OutPutJson = MReader["jsonoutput"].ToString();
                            }
                        }
                    }
                }
            }
            return OutPutJson;
        }
        public string InsertOrganizationStructure(OrganizationStructure organizationstructure)
        {
            string IResult = "SME";
            int Icount = 0;
            try
            {
                for (int i = 1; i <= organizationstructure.OrganizationTitle.Count - 1; i++)
                {
                    using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[IUD_InsertOrganizationStructure]"))
                    {

                        SQLServer.AddInParameter(dbCommand, "@Organiztionid", DbType.Int32, organizationstructure.OrganizationTitle[i].key);
                        SQLServer.AddInParameter(dbCommand, "@OrganizationName", DbType.String, organizationstructure.OrganizationTitle[i].name);
                        SQLServer.AddInParameter(dbCommand, "@OrganizationAbrivation", DbType.String, organizationstructure.OrganizationTitle[i].title);
                        SQLServer.AddInParameter(dbCommand, "@ParentOrganization", DbType.Int32, organizationstructure.OrganizationTitle[i].parent);
                        using (DbConnection con = SQLServer.CreateConnection())
                        {
                            Icount = (int)SQLServer.ExecuteScalar(dbCommand);
                            IResult = "ISUS";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                IResult = "ERR";
            }
            return IResult;
        }
        public List<Departments> GetDepartMents()
        {
            List<Departments> departments = new List<Departments>();
            try
            {
                using (DbCommand dbCommand = SQLServer.GetStoredProcCommand("[dbo].[GET_ChurchDepartMents]"))
                {
                    using (DbConnection con = SQLServer.CreateConnection())
                    {
                        using (IDataReader OReader = SQLServer.ExecuteReader(dbCommand))
                        {
                            if (OReader == null)
                            {
                                return departments;
                            }
                            while (OReader.Read())
                            {
                                Departments Odepartment = new Departments();
                                Odepartment.DepartmentName = OReader["OrganizationName"].ToString();
                                Odepartment.Departmentsid = (int)OReader["Organiztionid"];
                                departments.Add(Odepartment);
                            }
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
            }
            return departments;
        }
    }
}
